package timing.ukulele.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.flow.FlowData;
import timing.ukulele.common.item.ItemData;
import timing.ukulele.portal.client.FlowClient;
import timing.ukulele.portal.client.FormClient;
import timing.ukulele.portal.client.ItemClient;
import timing.ukulele.portal.persistent.System;
import timing.ukulele.portal.service.SystemService;
import feign.form.FormData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/system")
@Api("业务系统接口")
public class SystemController {

    private final SystemService systemService;
    @Resource
    private  FormClient formClient;
    @Resource
    private  ItemClient itemClient;
    @Resource
    private  FlowClient flowClient;

    public SystemController(SystemService systemService) {
        this.systemService = systemService;
    }

    @GetMapping("/page")
    @ApiOperation("分页获取模板列表")
    public ResponseResult<IPage<System>> page(
            @ApiParam(value = "查询名称") @RequestParam(required = false) String name,
        @ApiParam(value = "系统id/租户id") @RequestParam(required = false) Long id ,
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        if (name != null) {
            map.put("name", name);
        }
        if (id != null) {
            map.put("useId", id);
        }
        return ResponseResult.success(systemService.getPage(map, pageNum, pageSize));
    }

    @PostMapping("/create")
    @ApiOperation("保存业务系统信息")
    @ApiImplicitParam(name = "system", value = "业务系统对象", required = true, dataType = "System")
    public ResponseResult<System> create(
            @ApiParam(value = "业务系统", required = true) @RequestBody System system) {
        if (system.getName() == null || system.getCode() == null) {
            return ResponseResult.error("请将信息填写完整!");
        }
        System systemByCode = systemService.getByCode(system.getCode());
        if (systemByCode != null) {
            return ResponseResult.error("系统编码已被占用,请重新编写!");
        }
        systemService.save(system);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改业务系统信息")
    @ApiImplicitParam(name = "system", value = "业务系统对象", required = true, dataType = "System")
    public ResponseResult<System> update(
            @ApiParam(value = "业务系统", required = true) @RequestBody System system) {
        systemService.updateById(system);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除业务系统")
    public ResponseResult<System> remove(@ApiParam(value = "业务系统id", required = true, example = "1") @RequestParam Long id) {
        //删除之前先校验是否已经被flow, item, form使用
        ResponseResult<List<FlowData>> flowDataResponseResult = flowClient.getBySystemId(id);
        ResponseResult<List<ItemData>> itemDataResponseResult = itemClient.getBySystemId(id);
        ResponseResult<List<FormData>> formDataResponseResult = formClient.getBySystemId(id);
        if (CollectionUtils.isEmpty(flowDataResponseResult.getData()) && CollectionUtils.isEmpty(itemDataResponseResult.getData()) && CollectionUtils.isEmpty(formDataResponseResult.getData())) {
            systemService.removeById(id);
            return ResponseResult.success();
        }
        return ResponseResult.error("删除失败,该业务系统正在使用!");
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("查找业务系统")
    public ResponseResult<System> find(@ApiParam(value = "业务系统id", required = true, example = "1") @PathVariable("id") Long id) {
        System system = systemService.getBaseMapper().selectById(id);
        return ResponseResult.success(system);
    }

    @GetMapping("/findAll")
    @ApiOperation("获取所有业务系统")
    public ResponseResult<List<System>> findAll(@ApiParam(value = "系统权限ID") @RequestHeader(Constant.SYSTEM) String systemIds) {
        return ResponseResult.success(systemService.findAllWithPermission(systemIds));
    }

    @PostMapping("/validateSystemCode")
    @ApiOperation("校验业务系统编码是否勋在")
    public ResponseResult<Boolean> validateSystemCode(
            @ApiParam("系统编码") @RequestParam String code,
            @ApiParam("更新系统的ID") @RequestParam Long updateSystemId) {
        System system = systemService.getByCode(code);
        if (system != null) {
            if (system.getId().equals(updateSystemId)) {
                return ResponseResult.success(true);
            }
            return ResponseResult.success(false);
        }
        return ResponseResult.success(true);
    }

}
